package com.mangosteen.interviewji.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mangosteen.interviewji.common.ErrorCode;
import com.mangosteen.interviewji.constant.CommonConstant;
import com.mangosteen.interviewji.exception.ThrowUtils;
import com.mangosteen.interviewji.mapper.QuestionAssociateMapper;
import com.mangosteen.interviewji.model.dto.questionAssociate.QuestionAssociateQueryRequest;
import com.mangosteen.interviewji.model.entity.QuestionAssociate;
import com.mangosteen.interviewji.model.entity.User;
import com.mangosteen.interviewji.model.vo.QuestionAssociateVO;
import com.mangosteen.interviewji.model.vo.UserVO;
import com.mangosteen.interviewji.service.QuestionAssociateService;
import com.mangosteen.interviewji.service.UserService;
import com.mangosteen.interviewji.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 题目关联服务实现
 *
 */
@Service
@Slf4j
public class QuestionAssociateServiceImpl extends ServiceImpl<QuestionAssociateMapper, QuestionAssociate> implements QuestionAssociateService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param questionAssociate
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validQuestionAssociate(QuestionAssociate questionAssociate, boolean add) {
//        ThrowUtils.throwIf(questionAssociate == null, ErrorCode.PARAMS_ERROR);
//        // todo 从对象中取值
//        String title = questionAssociate.getTitle();
//        // 创建数据时，参数不能为空
//        if (add) {
//            // todo 补充校验规则
//            ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
//        }
//        // 修改数据时，有参数则校验
//        // todo 补充校验规则
//        if (StringUtils.isNotBlank(title)) {
//            ThrowUtils.throwIf(title.length() > 80, ErrorCode.PARAMS_ERROR, "标题过长");
//        }
    }

    /**
     * 获取查询条件
     *
     * @param questionAssociateQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<QuestionAssociate> getQueryWrapper(QuestionAssociateQueryRequest questionAssociateQueryRequest) {
        QueryWrapper<QuestionAssociate> queryWrapper = new QueryWrapper<>();
        if (questionAssociateQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Long id = questionAssociateQueryRequest.getId();
        Long notId = questionAssociateQueryRequest.getNotId();
        String title = questionAssociateQueryRequest.getTitle();
        String content = questionAssociateQueryRequest.getContent();
        String searchText = questionAssociateQueryRequest.getSearchText();
        String sortField = questionAssociateQueryRequest.getSortField();
        String sortOrder = questionAssociateQueryRequest.getSortOrder();
        List<String> tagList = questionAssociateQueryRequest.getTags();
        Long userId = questionAssociateQueryRequest.getUserId();
        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        // JSON 数组查询
        if (CollUtil.isNotEmpty(tagList)) {
            for (String tag : tagList) {
                queryWrapper.like("tags", "\"" + tag + "\"");
            }
        }
        // 精确查询
        queryWrapper.ne(ObjectUtils.isNotEmpty(notId), "id", notId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取题目关联封装
     *
     * @param questionAssociate
     * @param request
     * @return
     */
    @Override
    public QuestionAssociateVO getQuestionAssociateVO(QuestionAssociate questionAssociate, HttpServletRequest request) {
        // 对象转封装类
        QuestionAssociateVO questionAssociateVO = QuestionAssociateVO.objToVo(questionAssociate);


        return questionAssociateVO;
    }

    /**
     * 分页获取题目关联封装
     *
     * @param questionAssociatePage
     * @param request
     * @return
     */
    @Override
    public Page<QuestionAssociateVO> getQuestionAssociateVOPage(Page<QuestionAssociate> questionAssociatePage, HttpServletRequest request) {
        List<QuestionAssociate> questionAssociateList = questionAssociatePage.getRecords();
        Page<QuestionAssociateVO> questionAssociateVOPage = new Page<>(questionAssociatePage.getCurrent(), questionAssociatePage.getSize(), questionAssociatePage.getTotal());
        if (CollUtil.isEmpty(questionAssociateList)) {
            return questionAssociateVOPage;
        }
        // 对象列表 => 封装对象列表
        List<QuestionAssociateVO> questionAssociateVOList = questionAssociateList.stream().map(questionAssociate -> {
            return QuestionAssociateVO.objToVo(questionAssociate);
        }).collect(Collectors.toList());

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选
        // 1. 关联查询用户信息


        // endregion

        questionAssociateVOPage.setRecords(questionAssociateVOList);
        return questionAssociateVOPage;
    }

}
